using System.Collections;
using UnityEngine;

public class WeaponAbility : MonoBehaviour
{
	[HideInInspector]
	public Transform tf;

	[HideInInspector]
	public Rigidbody2D rb;

	[HideInInspector]
	public BoxCollider2D COL_WeaponTouchCol;

	[HideInInspector]
	public SpriteRenderer sr;

	[HideInInspector]
	public TrailRenderer trailr;

	[HideInInspector]
	public ParticleSystemRenderer psr;

	[HideInInspector]
	public int kindOfWeapon;

	public int weaponID;

	public int price;

	public string equipType;

	public string equipName;

	public int attackPower;

	public int defencePower;

	public int attackRangeWidth;

	public int attackRangeHeight;

	private void Awake()
	{
		tf = base.transform;
		rb = GetComponent<Rigidbody2D>();
		if (equipType != "Shield")
		{
			COL_WeaponTouchCol = base.transform.Find("WeaponTouchCol").GetComponent<BoxCollider2D>();
		}
		sr = GetComponent<SpriteRenderer>();
		trailr = tf.GetChild(0).GetComponent<TrailRenderer>();
		psr = tf.GetComponentInChildren<ParticleSystemRenderer>();
	}

	public void Get(string who, Transform TF_InHand, int id)
	{
		weaponID = id;
		kindOfWeapon = Calc.KindOfWeapon(weaponID);
		if (equipType != "Shield")
		{
			COL_WeaponTouchCol.enabled = false;
		}
		sr.sortingLayerName = who;
		if (psr != null)
		{
			psr.sortingLayerName = who;
		}
		ChangeWeaponTrailShowingTime(0f);
		rb.bodyType = RigidbodyType2D.Static;
		rb.simulated = false;
		tf.parent = TF_InHand;
		rb.velocity = Vector2.zero;
		tf.localPosition = Vector2.zero;
		tf.localRotation = Quaternion.Euler(Vector3.zero);
		tf.localScale = new Vector2(1f, 1f);
		StartCoroutine(ShowTrailrWidth());
	}

	private IEnumerator ShowTrailrWidth()
	{
		yield return new WaitForSeconds(0.1f);
		trailr.widthMultiplier = 2f;
	}

	public void Drop()
	{
		tf.parent = Generator.instance.pool.TF_EquipContainerOnFloor;
		rb.bodyType = RigidbodyType2D.Dynamic;
		rb.simulated = true;
		rb.velocity = new Vector2(Random.Range(-2f, 2f), Random.Range(15, 19));
		rb.angularVelocity = ((Random.Range(0, 2) != 0) ? (-1000) : 1000);
		ChangeWeaponTrailShowingTime(0f);
		trailr.widthMultiplier = 0f;
		DestroyWeaponTimer((equipType == "Shield") ? 1 : 3);
	}

	public void ChangeWeaponTrailShowingTime(float time)
	{
		trailr.time = time;
	}

	public void DestroyWeaponTimer(int timer)
	{
		StartCoroutine(IE_DestroyWeaponTimer(timer));
	}

	private IEnumerator IE_DestroyWeaponTimer(int timer)
	{
		yield return new WaitForSeconds(1f);
		if (equipType != "Shield")
		{
			Audio.instance.sfx[29].Play();
			COL_WeaponTouchCol.enabled = true;
			Generator.instance.SpawnObj(24, new Vector2(COL_WeaponTouchCol.transform.position.x, COL_WeaponTouchCol.transform.position.y), new float[0], COL_WeaponTouchCol.transform);
		}
		yield return new WaitForSeconds(timer);
		DestroyWeapon();
	}

	public void DestroyWeapon()
	{
		Object.Destroy(base.gameObject);
	}

	private void OnCollisionEnter2D(Collision2D collision)
	{
		if (rb.velocity.y > 2f)
		{
			Audio.instance.sfx[27].Play();
		}
	}
}
